<?php
class ControllerModuleBossBoughtTogether extends Controller {
	public function index($setting) {
		$this->language->load('module/boss_boughttogether');
        
        $this->data['status'] = $setting['status'];
 
      	$this->data['heading_title'] = $this->language->get('heading_title');
        
        $this->data['text_all_select'] = $this->language->get('text_all_select');
        $this->data['text_no_product'] = $this->language->get('text_no_product');
        $this->data['text_item'] = $this->language->get('text_item');
				
		$this->data['button_cart'] = $this->language->get('button_cart');
        
        $this->document->addStyle('catalog/view/theme/default/stylesheet/boss_boughttogether.css');
        
        $this->document->addScript('catalog/view/javascript/boss_boughttogether.js');
        
        $this->load->model('module/boss_boughttogether');
		
		$this->load->model('tool/image');

		$this->data['products'] = array();
        
        $total_old = 0;
        
        $total = 0;
        
        if (isset($this->request->get['product_id'])) {
			$product_id = (int)$this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		$this->load->model('catalog/product');
		
		$product_info = $this->model_catalog_product->getProduct($product_id);
        
        if ($product_info) {
            
            $this->load->model('tool/image');
			
			if ($product_info['image']) {
				$this->data['thumb'] = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
			} else {
				$this->data['thumb'] = '';
			}
            if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                $price = $this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));
				$this->data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$this->data['price'] = false;
			}
						
			if ((float)$product_info['special']) {
			     $price = $this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax'));
				$this->data['price'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
			}
        }
        
        $this->data['href'] = $this->url->link('product/product', 'product_id=' . $product_id);
        
        $this->data['name'] = $product_info['name'];
        
        $this->data['product_id'] = $product_id;
        
        $this->data['price_old'] = $price;
        
        $total_old = $total_old + $price;

		$results = $this->model_module_boss_boughttogether->getProductBoughtTogether($product_id);
        
        if(isset($results)){
    		foreach ($results as $result) {
    			if ($result['image']) {
    				$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
    			} else {
    				$image = false;
    			}
    			
    			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
    			     $price_old = $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'));
                     $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
    			} else {
                    $price = false;
    			}
    						
    			if ((float)$result['special']) {
    			     $price_old = $this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'));
    			     $price = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
    			} 
    			
    			if ($this->config->get('config_review_status')) {
    				$rating = $result['rating'];
    			} else {
    				$rating = false;
    			}
    							
    			$this->data['products'][] = array(
    				'product_id' => $result['product_id'],
    				'thumb'   	 => $image,
    				'name'    	 => $result['name'],
    				'price'   	 => $price,
                    'price_old'   	 => $price_old,
    				'rating'     => $rating,
    				'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
    				'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id'])
    			);
                
                $total_old = $total_old + $price_old;
    		}
        
        } else {
            $this->data['status'] = 0;
        }
        
        $this->data['success'] = sprintf($this->language->get('text_success'), $this->url->link('checkout/cart'));
        
        $total = $this->currency->format($total_old);
        
        //$total = $total_old;
        
        $this->data['total'] = $total;

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/boss_boughttogether.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/boss_boughttogether.tpl';
		} else {
			$this->template = 'default/template/module/boss_boughttogether.tpl';
		}

		$this->render();
	}
}
?>